package ru.efimmatytsin.tweeterella;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import ru.efimmatytsin.tweeterella.entities.Photo;
import ru.efimmatytsin.tweeterella.entities.Tweet;
import ru.efimmatytsin.tweeterella.entities.User;
import ru.efimmatytsin.tweeterella.holders.UserInformationModelHolder;
import ru.efimmatytsin.tweeterella.services.PhotoService;
import ru.efimmatytsin.tweeterella.services.TweetService;
import ru.efimmatytsin.tweeterella.services.UserService;
import ru.efimmatytsin.tweeterella.viewmodels.PostViewModel;
import ru.efimmatytsin.tweeterella.viewmodels.UserViewModel;

import java.math.BigInteger;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Efim Matytsin on 22.02.15.
 */
@Controller
@RequestMapping("/profile")
public class UserProfileController {

    @Autowired
    private UserInformationModelHolder userInformationModelHolder;

    @Autowired
    private UserService userService;

    @Autowired
    private TweetService tweetService;

    @Autowired
    private PhotoService photoService;

    @Autowired
    private ConversionService conversionService;

    @RequestMapping(value = "/id{idNumber}",method = RequestMethod.GET)
    public String initializeView(Model model, @PathVariable("idNumber") String idNumber) {
        if (userInformationModelHolder.getUser() == null) {
            return "landing";
        }
        model.addAttribute("user",userService.getUser(new BigInteger(idNumber), UserViewModel.class));
        return "profile";
    }

    @RequestMapping(value = "/posts/{idOwner}", method = RequestMethod.GET)
    @ResponseBody
    public List<PostViewModel> getPosts(Model model, @PathVariable("idOwner") String idOwner) {
        List<PostViewModel> results = new ArrayList<PostViewModel>();
        List<Tweet> tweets = tweetService.getUserTweets(userService.getUser(new BigInteger(idOwner),User.class));
        for (Tweet t : tweets) {
            results.add(conversionService.convert(t,PostViewModel.class));
        }
        List<Photo> photos = photoService.getUserPhotos(userService.getUser(new BigInteger(idOwner),User.class));
        for(Photo p: photos){
            results.add(conversionService.convert(p,PostViewModel.class));
        }
        return results;
    }

    @RequestMapping(value="/posts/new", method=RequestMethod.GET)
    @Transactional
    @ResponseBody
    public PostViewModel addPost(Model model,@RequestParam String message){
        Tweet tweet = new Tweet();
        tweet.setText(message);
        tweet.setCreated(new Date(Calendar.getInstance().getTimeInMillis()));
        tweet.setFromUser(userInformationModelHolder.getUser());
        tweetService.saveTweet(tweet);
        return conversionService.convert(tweet,PostViewModel.class);
    }
}
